import LogUtil from "../../utils/LogUtil";
import PopupBase, { HideFuncType } from "./PopupBase";

const { ccclass, property } = cc._decorator;

/**
 * 确认弹窗（PopupBase 使用示例）
 */
@ccclass
export default class ConfirmPopup extends PopupBase<ConfirmPopupOptions> {

    @property(cc.Label)
    private titleLabel: cc.Label = null;

    @property(cc.Label)
    private contentLabel: cc.Label = null;

    @property(cc.Node)
    private confirmBtn: cc.Node = null;

    protected onLoad() {
        this.registerEvent();
    }

    protected onDestroy() {
        this.unregisterEvent();
    }

    private registerEvent() {
        this.confirmBtn.on(cc.Node.EventType.TOUCH_END, this.onConfirmBtnClick, this);
    }

    private unregisterEvent() {
        this.confirmBtn.targetOff(this);
    }

    protected init(options: ConfirmPopupOptions) {

    }

    protected updateDisplay(options: ConfirmPopupOptions): void {
        this.titleLabel.string = options.title?options.title:"温馨提示";
        this.contentLabel.string = options.content?options.content:"";
    }

    protected onConfirmBtnClick() {
        this.hideFuncType = HideFuncType.confirm;
        this.hide();
    }

    protected onHide(suspended: boolean) { 
        if(this.hideFuncType == HideFuncType.confirm){
            this.options.confirmCallback && this.options.confirmCallback();
        }else if(this.hideFuncType == HideFuncType.cancel){
            LogUtil.log('隐藏弹窗取消方法回调');
            this.options.cancelCallback && this.options.cancelCallback();
        }
    }

}

/** 
 * 确认弹窗选项 
 * @param title 标题
 * @param content 内容
 * @param confirmCallback 确认回调
 * @param cancelCallback 取消回调
 */
export interface ConfirmPopupOptions {
    title: string;
    content: string;
    confirmCallback?: Function;
    cancelCallback?: Function;
}
